package com.njit.WeStudy.dao;
import org.apache.ibatis.jdbc.SQL;
/**
 * @author yuruiqiao
 * @date 2020/2/1
 * @description 各种查询接口的实现类
 */
public class QueryBuilder{
    public String query(String info,String str){
        return new SQL(){
            {
                SELECT("*");
                FROM("user");
                WHERE(info+" = '"+str+"'");
            }
        }.toString();
    }
    public String likeQueryByInvitationId(int invitationId){
        return new SQL(){
            {
                SELECT("*");
                FROM("`like`");
                WHERE("is_liked = true and invitation_id="+invitationId);
            }
        }.toString();
    }
    public String likeQueryByUserId(int userId) {
        return new SQL() {
            {
                SELECT("*");
                FROM("`like`");
                WHERE("is_liked = true and user_id = "+userId);
            }
        }.toString();
    }
    public String queryMyselfLike(int invitationId,int userId) {
        return new SQL() {
            {
                SELECT("*");
                FROM("`like`");
                WHERE("invitation_id=" + invitationId+" and user_id = "+userId);
            }
        }.toString();
    }
    //查询当前帖子(其它用户)的收藏(关注)数量(要加.size())
    public String starQueryByTargetId(int targetId,String targetType){
        return new SQL(){
            {
                SELECT("*");
                FROM("`star`");
                WHERE("is_stared = true and target_id="+targetId+" and target_Type='"+targetType+"'");
            }
        }.toString();
    }
    //查询当前用户的收藏(关注)帖子(其它用户)数量(要加.size())
    public String starQueryByUserId(int userId,String targetType) {
        return new SQL() {
            {
                SELECT("*");
                FROM("`star`");
                WHERE("is_stared = true and user_id = "+userId+" and target_type='"+targetType+"'");
            }
        }.toString();
    }
    //查看当前用户对当前帖子(其它用户)的收藏(关注)状态(加上.getIsStared())
    public String queryMyselfStar(int targetId,String targetType,int userId) {
        return new SQL() {
            {
                SELECT("*");
                FROM("`star`");
                WHERE("target_id=" + targetId+" and target_type = '"+targetType+"' and user_id="+userId);
            }
        }.toString();
    }
    //查询当前用户的评论
    public String commentQueryByUserId(int userId){
        return new SQL(){
            {
                SELECT("*");
                FROM("comment");
                WHERE("user_id="+userId);
            }
        }.toString();
    }

    //查询到当前帖子下的所有评论
    public String commentQueryByInvitationId(int invitationId){
        return new SQL(){
            {
                SELECT("*");
                FROM("comment");
                WHERE("invitation_id="+invitationId);
            }
        }.toString();
    }

    //查询到当前帖子下的所有评论,包含用户名,头像,目标用户名
    public String queryCommentViewPlusByInvitationId(int invitationId){
        return new SQL(){
            {
                SELECT("id, comment.user_id, invitation_id, target_type,storey_id, target_user_id, comment_time, content,support_number,"
                        + "fromUser.user_name,fromUser.head_image,toUser.user_name as target_user_name");
                FROM("comment,user as fromUser,user as toUser");
                WHERE(" comment.user_id=fromUser.user_id and target_user_id=toUser.user_id and " +
                        "invitation_id="+invitationId);
            }
        }.toString();
    }
    //根据storeyId查询所有的评论
    public String commentQueryByStoreyId(int storeyId){
        return new SQL(){
            {
                SELECT("*");
                FROM("comment");
                WHERE("storey_id="+storeyId);
            }
        }.toString();
    }
    //查询当前帖子下我的评论
    public String QueryMySelfComments(int invitationId,int userId){
        return new SQL(){
            {
                SELECT("*");
                FROM("comment");
                WHERE("invitation_id="+invitationId+" and user_id ="+userId);
            }
        }.toString();
    }

    //查询当前帖子的跟帖(包含用户名、头像)
    public String selectViewByInvitationId(int invitationId){
        return new SQL() {
            {
                SELECT("storey_id, storey.user_id, invitation_id, create_time, content, support_number, comment_number,user_name,head_image");
                FROM("storey,user");
                WHERE("storey.user_id = user.user_id and invitation_id="+invitationId);
            }
        }.toString();
    }

    //查询当前帖子的跟帖(不包含用户名、头像)
    public String storeyQueryByInvitationId(int invitationId){
        return new SQL(){
            {
                SELECT("*");
                FROM("storey");
                WHERE("invitation_id="+invitationId);
            }
        }.toString();
    }

    //查询当前用户的跟帖
    public String storeyQueryByUserId(int userId){
        return new SQL(){
            {
                SELECT("*");
                FROM("storey");
                WHERE("user_id="+userId);
            }
        }.toString();
    }
    //查询当前用户对当前帖子的评论
    public String queryMyselfStoreys(int userId,int invitationId){
        return new SQL(){
            {
                SELECT("*");
                FROM("storey");
                WHERE("user_id="+userId+" and invitation_id="+invitationId);
            }
        }.toString();
    }

    //查询当前层或用户的support
    public String supportQueryByTargetId(int targetId,String targetType){
        return new SQL(){
            {
                SELECT("*");
                FROM("support");
                WHERE("is_supported=true and target_id="+targetId+" and target_type='"+targetType+"'");
            }
        }.toString();
    }

    //查询当前用户对目标是否支持(加.getIsSupported())
    public String QueryMyselfSupport(int targetId,String targetType,int userId){
        return new SQL(){
            {
                SELECT("*");
                FROM("support");
                WHERE("target_id="+targetId+" and target_type='"+targetType+"' and user_id="+userId);
            }
        }.toString();
    }
    //查看当前用户所有支持的用户或者跟帖的数量(加上.size())
    public String supportQueryByUserId(int userId,String targetType){
        return new SQL(){
            {
                SELECT("*");
                FROM("support");
                WHERE("is_supported=true  and target_type='"+targetType+"' and user_id="+userId);
            }
        }.toString();
    }
    //查看当前帖子的support
    public String supportQueryByInvitationId(int invitationId){
        return new SQL(){
            {
                SELECT("*");
                FROM("support");
                WHERE("is_supported=true and invitation_id="+invitationId);
            }
        }.toString();
    }
    //查看当前帖子的当前用户的support,用于分辨用户是否support某个storey或者comment
    public String mySupportIdsQueryByInvitationId(int invitationId,int userId){
        return new SQL(){
            {
                SELECT("*");
                FROM("support");
                WHERE("is_supported=true and invitation_id="+invitationId+" and user_id="+userId);
            }
        }.toString();
    }
    //查看当前帖子的当前用户的comment,用于分辨用户是否comment某个storey或者comment
    public String myCommentIdsQueryByInvitationId(int invitationId,int userId){
        return new SQL(){
            {
                SELECT("*");
                FROM("comment");
                WHERE("invitation_id="+invitationId+" and user_id="+userId);
            }
        }.toString();
    }
    //根据userId查询参与班级表
    public String joinClassQueryByUserId(int userId){
        return new SQL(){
            {
                SELECT("*");
                FROM("join_class");
                WHERE("user_id="+userId);
            }
        }.toString();
    }
    //根据classId查询学生
    public  String joinClassQueryByClassId(int classId){
        return new SQL(){
            {
                SELECT("id, join_class.user_id, class_id, join_time, leave_time, is_top,user_name,head_image");
                FROM("join_class,user");
                WHERE("user.user_id=join_class.user_id and class_id="+classId);
            }
        }.toString();
    }
    //根据查询包含用户名和头像的class
    public String selectViewByPrimaryKey(int classId){
        return new SQL(){
            {
                SELECT("class_id, class_name, master_id, class_type, description, create_time, delete_time, is_deleted,user_name,head_image");
                FROM("class,user");
                WHERE("class.master_id=user.user_id and class_id="+classId);
            }
        }.toString();
    }
    //根据classId查询InviteCode
    public String inviteCodeQueryByClassId(int classId){
        return new SQL(){
            {
                SELECT("*");
                FROM("invite_code");
                WHERE("class_id="+classId);
            }
        }.toString();
    }
}
